//
//  ZCTelephoneVC.m
//  zctx
//
//  Created by apple on 15/12/3.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCTelephoneVC.h"

@interface ZCTelephoneVC ()
{
    UITextField *textField;
}
@end

@implementation ZCTelephoneVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCTelephoneVC"];
    [MTA trackPageViewBegin:@"ZCTelephoneVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCTelephoneVC"];
    [MTA trackPageViewEnd:@"ZCTelephoneVC"];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"手机号码";
    self.view.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    [self initnavegation];
    [self initNameView];
}
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)initNameView{
    
    UIView *btnView = [[UIView alloc]init];
    [self.view addSubview:btnView];
    [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(64);
        make.right.equalTo(self.view).with.offset(0);
        make.left.equalTo(self.view).with.offset(0);
        make.height.mas_equalTo(@200);
    }];
    btnView.backgroundColor = [UIColor whiteColor];
    
    textField = [[UITextField alloc]init];
    [btnView addSubview:textField];
    textField.keyboardType = UIKeyboardTypeNumberPad;
    textField.font = [UIFont systemFontOfSize:15];
    [textField setBackgroundColor:[UIColor colorWithHexString:@"f2f2f2"]];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@(30));
        make.left.equalTo(btnView.left).with.offset(30);
        make.right.equalTo(btnView.right).with.offset(-30);
        make.top.equalTo(btnView.top).with.offset(50);
    }];
    textField.placeholder = @"请输入手机号码";
    textField.text = UserModel.mobile;

    UIButton *confirmButton = [UIButton new];
    [btnView addSubview:confirmButton];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textField.bottom).with.offset(30);
        make.right.equalTo(btnView).with.offset(-30);
        make.left.equalTo(btnView).with.offset(30);
        make.height.mas_equalTo(@44);
    }];
    [confirmButton setTitle:@"确认" forState:UIControlStateNormal];
    confirmButton.backgroundColor = MainColor;
    confirmButton.layer.cornerRadius =4;
    [confirmButton addTarget:self action:@selector(confirmButtonClick) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark 点击确认
-(void)confirmButtonClick{
    if (!(textField.text.length==11)) {
        [SVProgressHUD showInfoWithStatus:@"手机号必须11位"];
        return;
    }
    [NetworkManager requestModifymobileWithMid:UserModel.mid mobile:textField.text success:^(NSDictionary *ArrayResponse) {
        NSString *err = [NSString stringWithFormat:@"%@",[ArrayResponse objectForKey:@"err" ]];  // 1 失败 0  成功
        if ([err isEqualToString:@"0"]) {
            NSLog(@"成功");
             [SVProgressHUD showSuccessWithStatus:@"修改成功"];
              [UserModel setMobile:textField.text];
              [[NSNotificationCenter defaultCenter]postNotificationName:@"userinfo" object:nil userInfo:@{@"user":@"1"}];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            NSLog(@"操作失败");
            NSString *msg = [NSString stringWithFormat:@"%@",[ArrayResponse objectForKey:@"msg" ]];
            [SVProgressHUD showErrorWithStatus:msg];
        }
    } failure:^(NSError *error) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
